<template>
  <div class="registerDetail">
    <van-nav-bar :fixed="true" :placeholder="true" :title="$route.meta.title" left-text="返回" left-arrow @click-left="onClickLeft" />
    <!-- 表单 -->
    <div class="formData">
      <div class="fail_title">
        请选择本次事故车辆<span class="txt">{{ carNo }}</span
        >战败原因
      </div>
      <van-form @submit="formOnSubmit" ref="form">
        <van-radio-group v-model="result" class="fail_checkbox">
          <van-radio style="margin-bottom: 10px" v-for="(item, index) in taskFailedList" :key="index" :name="item.billId" shape="square">{{
            item.name
          }}</van-radio>
        </van-radio-group>
        <van-field v-model="remark" name="备注" label="" type="textarea" input-align="left" placeholder="备注" autosize />
      </van-form>
      <div class="submitBtn" @click="formOnSubmit">确认</div>
    </div>

    <div class="loading" v-if="loading">
      <van-loading size="40px" class="loadbox" color="#fff" text-color="#fff"> 提交中，请稍等... </van-loading>
      <div class="mask"></div>
    </div>
  </div>
</template>

<script>
import { Dialog, Field, Form, Loading, Notify, Radio, RadioGroup } from 'vant'
import outApi from '@/api/outAPi'
import commonApi from '@/api/commonApi'
import { getCookies } from '@/utils/util.js'

export default {
  name: 'registerDetail',
  components: {
    Field,
    Form,
    Loading,
    Notify,
    Dialog,
    RadioGroup,
    Radio,
  },
  data() {
    return {
      userId: getCookies('userId'),
      carNo: '',
      billId: '',
      taskFailedList: [],
      result: '',
      loading: false,
      remark: '',
    }
  },
  watch: {},

  mounted() {
    if (this.$route.query && this.$route.query.carNo && this.$route.query.billId) {
      this.billId = this.$route.query.billId
      this.carNo = this.$route.query.carNo
    }
    this.getTaskFailedList()
    localStorage.setItem('failBillId', this.billId)
  },

  methods: {
    // 获取战败
    getTaskFailedList() {
      commonApi
        .getBaseCode({
          key: '车辆未进厂原因',
          userId: this.userId,
        })
        .then((res) => {
          if (res.data) {
            this.taskFailedList = res.data
          }
        })
    },

    formOnSubmit() {
      let vm = this
      if (!this.result) {
        Notify({ type: 'danger', message: '请选择战败原因' })
        return
      }
      if (!this.remark) {
        Notify({ type: 'danger', message: '请输入备注' })
        return
      }
      // return;
      Dialog.confirm({
        title: '提示',
        message: `确定提交${vm.carNo}战败登记？`,
      })
        .then(() => {
          this.loading = true
          outApi
            .carAccidentTrackFailed({
              billId: this.billId,
              tunfixReasonId: this.result,
              remark: this.remark,
            })
            .then((res) => {
              if (res.code == 1) {
                Notify({ type: 'success', message: '登记成功' })
                // this.$router.go(-1)
                this.$route.params.failBillId = this.billId
                this.$router.back()
              }
              this.loading = false
            })
        })
        .catch(() => {
          this.loading = false
          console.log('取消')
        })
    },
    onClickLeft() {
      this.$route.params.failBillId = this.billId
      this.$router.back()
    },
  },
}
</script>

<style lang="less" scoped>
body {
  // background: #fff;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  padding-bottom: 1rem;
  background: #fff;

  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;
    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;
        .icon-right {
          margin-left: 0.2rem;
          color: #409eff;
        }
      }
      .hank {
        font-size: 0.28rem;
        color: #666;
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 0.28rem;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }
  .formData {
    background: #fff;
  }
}
/deep/.van-field__error-message {
  text-align: right;
}
.submitBtn {
  background: #409eff;
  color: #fff;
  width: 75%;
  margin: auto;
  text-align: center;
  padding: 0.25rem;
  border-radius: 5px;
  margin-top: 0.6rem;
  font-size: 0.3rem;
}
#container {
  overflow: hidden;
  width: 100%;
  height: 200px;
  margin: 0;
  font-family: '微软雅黑';
}
.loading {
  position: fixed;
  width: 100%;
  height: 100%;
  .loadbox {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 11;
  }
  .mask {
    background: rgba(0, 0, 0, 0.5);
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 10;
  }
}
.imgList {
  padding: 0.2rem;
  display: flex;
  .img_item {
    margin-right: 0.2rem;
  }
}
.fail_title {
  padding: 0.2rem;
  font-size: 0.28rem;
  color: #666;

  .txt {
    color: rgb(240, 41, 1);
    margin: 0 10px;
  }
}
.fail_checkbox {
  padding: 0.2rem;
  font-size: 0.28rem;
}
</style>
